package com.yfree.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.yfree.R;
import com.yfree.YConfig;
import com.yfree.application.YApplication;
import com.yfree.utils.net.YJsonKV;
import com.yfree.utils.net.YResponseUtils;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

public final class YCrashActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        int targetSdkVersion = getApplicationInfo().targetSdkVersion;
        if (targetSdkVersion > Build.VERSION_CODES.O) {
            getApplicationInfo().targetSdkVersion = Build.VERSION_CODES.O;
        }
        super.onCreate(savedInstanceState);
        getApplicationInfo().targetSdkVersion = targetSdkVersion;
        setTheme(R.style.ActivityDialogStyle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View contentView = new View(this);
        contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        contentView.setBackgroundColor(Color.TRANSPARENT);
        setContentView(contentView);
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        sendCrashInfo();
        showCrashBuilder();
    }

    private void sendCrashInfo() {
        if (TextUtils.isEmpty(getIntent().getStringExtra(YConfig.YBundleKey.yCrashJKID))) {
            return;
        }
        YJsonKV kv = new YJsonKV();
        kv.put("msg", getIntent().getStringExtra(YConfig.YBundleKey.yCrashMSG));
        AjaxParams params = new AjaxParams();
        params.put("jkid", getIntent().getStringExtra(YConfig.YBundleKey.yCrashJKID));
        params.put("jkparam", YResponseUtils.compress(kv));
        YApplication.getFinalHttp().post(getIntent().getStringExtra(YConfig.YBundleKey.yCrashIP), params, new AjaxCallBack<Object>() {
        });
    }

    private void showCrashBuilder() {
        if (!isFinishing()) {
            Object obj = getIntent().getSerializableExtra(YConfig.YBundleKey.yCrashRestartActivity);
            final Class restartActivity = (obj != null ? (Class) obj : null);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(getIntent().getStringExtra(YConfig.YBundleKey.yCrashAlertMSG));
            builder.setCancelable(false);
            builder.setPositiveButton(restartActivity != null ? "重新启动" : "退出应用", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (restartActivity != null)
                        startActivity(new Intent(YCrashActivity.this, restartActivity));
                    finish();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.setCancelable(false);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        }
    }
}
